import {messages} from "share/common";
import React from 'react';
import 'styles/request-and-expense-settings/compliance-rule-card.scss'
import {message, Switch, Spin} from "antd";
import invoiceManagementService from 'containers/request-and-expense-settings/invoice-management/invoice-management-service'

class ComplianceRule extends React.Component {

  state = {
    value: {},
    loading: {},
    loadingPage: false,
    dataList: []
  };

  cardType = {
    'COMPLETE_INFORMATION': {
      title: messages('expense-3.key115')/*发票信息完整性校验*/,
      precondition: messages('expense-3.key116')/*前置条件：发票查验成功或发票来源为OCR*/,
      description: (
        <div>
          <p className='compliance-rule-card-body-title'>{messages('expense-3.key117')/*功能概述：*/}</p>
          <p>
            {messages('expense-3.key118')/*1. 增值税发票缺失销售方公司印章*/}
            （{messages('expense-3.key119')/*增值税电子专用发票不校验*/}）
          </p>
          <p>{messages('expense-3.key120')/*2. 增值税发票购买方“抬头名称”、“纳税人识别号”不为空*/}</p>
          <p>{messages('expense-3.key121')/*3. 增值税专用发票购买方“抬头名称”、“纳税人识别号”、“地址电话”、“开户行及账号”不为空*/}</p>
        </div>
      )
    },
    'PRINT_COMPLIANCE': {
      title: messages('expense-3.key122')/*发票打印校验*/,
      precondition: messages('expense-3.key123')/*前置条件：发票来源为OCR且校验字段不为空*/,
      description: (
        <div>
          <p className='compliance-rule-card-body-title'>{messages('expense-3.key117')/*功能概述：*/}</p>
          <p>{messages('expense-3.key124')/*发票号码需与机打号码一致；发票代码需与发票打印编码一致*/}</p>
        </div>
      )
    },
    'RECEIPT_STAMP_CONSISTENCE': {
      title: messages('expense-3.key125')/*印章信息校验*/,
      precondition: messages('expense-3.key123')/*前置条件：发票来源为OCR且校验字段不为空*/,
      description: (
        <div>
          <p className='compliance-rule-card-body-title'>{messages('expense-3.key117')/*功能概述：*/}</p>
          <p>{messages('expense-3.key126')/*销售方公司印章内的税号需与发票销售方税收人识别号一致*/}</p>
        </div>
      )
    },
    'RELEVANT_PERSONNEL_COMPLIANCE': {
      title: messages('expense-3.key127')/*票面人员校验*/,
      precondition: messages('expense-3.key128')/*前置条件：发票查验成功 或 发票来源为OCR且校验字段不为空*/,
      description: (
        <div>
          <p className='compliance-rule-card-body-title'>{messages('expense-3.key117')/*功能概述：*/}</p>
          <p>{messages('expense-3.key129')/*1. 复核人与开票人不为同一人*/}</p>
          <p>{messages('expense-3.key130')/*2. 复核人与收款人不为同一人*/}</p>
          <p>{messages('expense-3.key131')/*3. 开票人与收款人不为同一人*/}</p>
        </div>
      )
    },
    'RECEIPT_FORM_VALID': {
      title: messages('expense-3.key132')/*发票联次校验*/,
      precondition: messages('expense-3.key123')/*前置条件：发票来源为OCR且校验字段不为空*/,
      description: (
        <div>
          <p className='compliance-rule-card-body-title'>{messages('expense-3.key117')/*功能概述：*/}</p>
          <p> {messages('expense-3.key133')/*增值税专用发票需使用“发票联”报销，不可使用抵扣联（增值税电子专用发票不校验）*/}</p>
        </div>
      )
    },
    'SPECIFIC_CATEGORY_CHECK': {
      title: messages('expense-3.key134')/*特定类别发票校验*/,
      precondition: messages('expense-3.key135')/*前置条件：发票查验成功或发票来源为OCR且校验字段不为空*/,
      description: (
        <div>
          <p className='compliance-rule-card-body-title'>{messages('expense-3.key117')/*功能概述：*/}</p>
          <p>{messages('expense-3.key136')/*1. 商品服务名称包含“成品油”，“成品油标记”不能为空*/}</p>
          <p>{messages('expense-3.key137')/*2. 商品服务名称包含“成品油”，相应发票行的“单位”需包含“升”或“吨”*/}</p>
          <p>{messages('expense-3.key138')/*3. 商品服务名称包含“成品油”，相应发票行的“数量”不等于“0”且不能为空*/}</p>
        </div>
      )
    },
    'REMARK_CHECK': {
      title: messages('expense-3.key139')/*备注栏校验*/,
      precondition: messages('expense-3.key135')/*前置条件：发票查验成功或发票来源为OCR且校验字段不为空*/,
      description: (
        <div>
          <p className='compliance-rule-card-body-title'>{messages('expense-3.key117')/*功能概述：*/}</p>
          <p>{messages('expense-3.key140')/*1. 商品服务名称包含“货物运输服务”时，备注栏需包含“%起运%到达%车种%车号%货物%”*/}</p>
          <p>{messages('expense-3.key141')/*2. 商品服务名称包含“建筑服务”时，备注栏需包含“%项目地点%项目名称%”*/}</p>
          <p>{messages('expense-3.key142')/*3. 商品服务名称包含“差额征税”时，备注栏需包含“差额征税”*/}</p>
          <p>{messages('expense-3.key143')/*4. 商品服务名称包含“销售不动产”或“出租不动产”时，备注栏不能为空*/}</p>
          <p>{messages('expense-3.key144')/*5. 商品服务名称包含“销售预付卡”时，备注栏不能为空*/}</p>
          <p>{messages('expense-3.key145')/*6. 商品服务名称包含“保险”时，备注栏不能为空*/}</p>
          <p>{messages('expense-3.key146')/*7. 发票“代开”标记不为空时，备注栏不能为空*/}</p>
        </div>
      )
    }
  };

  componentWillMount() {
    this.getList();
  }

  getList = () => {
    this.setState({ loadingPage: true });
    invoiceManagementService.getComplianceDetails(this.props.id).then(res => {
      let value = {};
      res.data.map(i => value[i.code] = (i.enabled === 'Y'));
      this.setState({ value, dataList: res.data, loadingPage: false });
    })
  };

  onChange = (checked, type) => {
    let selectedData = this.state.dataList.filter(i => i.code === type);
    let params = {
      "complianceSetup": {
        "code": type,
        "enabled": checked ? "Y" : "N",
        "id": selectedData.length ? selectedData[0].id : null
      },
      "receiptCheckOptId": this.props.id
    };
    this.setState({ loading: Object.assign({}, this.state.loading, {[type]: true})});
    invoiceManagementService.saveComplianceDetails(params).then(res => {
      message.success(messages('common.operate.success')/*操作成功*/);
      this.setState({
        value: Object.assign({}, this.state.value, {[type]: checked}),
        loading: Object.assign({}, this.state.loading, {[type]: false})
      }, () => {
        if(selectedData.length && !selectedData[0].id) this.getList();
      });
    }).catch(e => {
      message.success(messages('common.operate.filed')/*操作失败*/);
      this.setState({ loading: Object.assign({}, this.state.loading, {[type]: false})});
    })
  };

  render () {
    const { value, loading, loadingPage } = this.state;
    const { cardList } = this.props;
    return (
      <Spin spinning={loadingPage}>
        {cardList.map(type => (
          <div className='compliance-rule-card'>
            <div className='compliance-rule-card-head'>
              <p className='compliance-rule-card-head-title'>{this.cardType[type].title}</p>
              <p className='compliance-rule-card-head-desc'>{this.cardType[type].precondition}</p>
              <label className='compliance-rule-card-head-label'>
                <Switch
                  size='small'
                  id={type}
                  checked={value[type]}
                  loading={loading[type]}
                  onChange={(checked) => this.onChange(checked, type)}
                />
                <span className='compliance-rule-card-head-label-span'>{value[type] ? messages('expense-3.key61')/*启用temp*/ : messages('expense-3.key62')/*禁用temp*/}</span>
              </label>
            </div>
            <div className='compliance-rule-card-body'>
              {this.cardType[type].description}
            </div>
          </div>
        ))}
      </Spin>
    )
  }

}

ComplianceRule.defaultProps = {
  cardList: [
    "COMPLETE_INFORMATION", //发票信息完整性校验
    "PRINT_COMPLIANCE", //发票打印校验
    "RECEIPT_STAMP_CONSISTENCE", //印章信息校验
    "RELEVANT_PERSONNEL_COMPLIANCE",//票面人员校验
    "RECEIPT_FORM_VALID", //发票联次校验
    "SPECIFIC_CATEGORY_CHECK", //特定类别发票校验
    "REMARK_CHECK" //发票备注栏校验
  ]
};

export default ComplianceRule
